package com.zhaorui.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhaorui.aop.Annotation2;
import com.zhaorui.entity.ActivityTitle;
import com.zhaorui.entity.ActivityUser;
import com.zhaorui.entity.ZR;
import com.zhaorui.form.TitleAddForm;
import com.zhaorui.form.TitleForm;
import com.zhaorui.form.UserAddForm;
import com.zhaorui.form.UserQueryForm;
import com.zhaorui.service.ActivityTitleService;
import com.zhaorui.service.ActivityUserService;
import com.zhaorui.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/activityTitle")
@Slf4j
@CrossOrigin
@Api(tags = "活动管理接口")
public class ActivityTitleController {
    @Autowired
    private ActivityTitleService activityTitleService;
    @Autowired
    private RedisTemplate redisTemplate;
    // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
    @Value("${OSS.endpoint}")
    String endpoint;
    // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
    @Value("${OSS.accessKeyId}")
    String accessKeyId;
    @Value("${OSS.accessKeySecret}")
    String accessKeySecret;
    // 填写Bucket名称，例如examplebucket。
    @Value("${OSS.bucketName}")
    String bucketName;

    /**
     * 综合查询
     *
     * @param titleForm
     * @param currentPage
     * @param pagesize
     * @return
     */
    @GetMapping("/queryTitle")
    @ApiOperation("活动综合查询")
    public ZR queryTitle(TitleForm titleForm, @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage, @RequestParam(name = "pagesize", defaultValue = "5") Integer pagesize) {
        log.info ("综合查询:{}", titleForm);
//        ActivityUser activityUser = new ActivityUser ();
        ActivityTitle activityTitle = new ActivityTitle ();
        //复制vo对象接收的参数到DTO对象中
        BeanUtil.copyProperties (titleForm, activityTitle);
        //分页对象
        Page page = new Page (currentPage, pagesize);
//        List<HashMap> users = activityUserService.queryUsers (activityUser);
        List<HashMap> titles = activityTitleService.queryTitle (activityTitle);
        PageVo pageVo = new PageVo ();
        pageVo.setResult (titles);
        pageVo.setTotal (page.getTotal ());
        return new ZR (1000, "查询成功", titles);
    }


    /**
     * 添加
     *
     * @param titleAddForm
     * @return
     */
    @PostMapping("/saveTitle")
    @ApiOperation("增加")
    @Transactional//事务
    @Annotation2(tableName = "activity_title", optionName = "增加")
    public ZR saveEmp(@Valid @RequestBody TitleAddForm titleAddForm) {
        log.info ("开始新增*********************************");
//        ActivityUser activityUser = new ActivityUser ();
        ActivityTitle activityTitle = new ActivityTitle ();
        BeanUtil.copyProperties (titleAddForm, activityTitle);
        activityTitleService.save (activityTitle);
        return new ZR (1000, "增加成功", activityTitle);
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteTitle/{id}")
    @ApiOperation("删除")
    @Annotation2(tableName = "activity_title", optionName = "删除", primaryKeyName = "id")
    @Transactional
    public ZR deleteTitle(@PathVariable Integer id) {
        log.info ("开始删除:{}", id);
        boolean state = activityTitleService.delete (id);
        if (state) {
            //删除缓存;就是Integer
            redisTemplate.delete (id + "");
            return new ZR (1000, "删除成功", id);
        }
        int i = 1 / 0;
        return new ZR (-1000, "删除失败", null);
    }


    /**
     * 修改
     *
     * @param activityTitle
     * @return
     */
    @Annotation2(tableName = "activity_title", optionName = "编辑")
    @PutMapping("/modifyTitle")
    @ApiOperation("编辑")
    @Transactional
    public ZR modifyTitle(@RequestBody ActivityTitle activityTitle) {
        log.info ("开始编辑:{}", activityTitle);
        if (activityTitleService.updateById (activityTitle)) {
            if (redisTemplate.hasKey (activityTitle.getId () + "")) {
                //存在删除缓存
                redisTemplate.delete (activityTitle.getId () + "");
            }
        }

        return new ZR (1000, "修改成功", activityTitle);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */

//    @PutMapping("/deleteAllTitle/{ids}")
//    @ApiOperation("批量删除")
//    @Annotation2(tableName = "activity_title", optionName = "批量删除", primaryKeyName = "ids")
//    @Transactional
//    public ZR deleteAllTitle(@PathVariable long[] ids) {
//        log.info ("开始批量删除:{}", ids);
//        int num = activityTitleService.delTitle (ids);
//        return new ZR (1000, "删除成功", num);
//    }




}
